<template>
  <ele-modal
    width="480px"
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="data?'编辑员工':'添加员工'"
    @update:visible="updateVisible"
  >
    <el-form
      size="small"
      class="ele-form-search"
      @keyup.enter.native="search"
      @submit.native.prevent
    >
      <el-form-item label="员工姓名:">
        <el-input
          clearable
          v-model="form.staffName"
          placeholder="请输入员工姓名"
        />
      </el-form-item>
      <el-form-item label="联系方式:">
        <el-input
          clearable
          :maxlength="11"
          v-model="form.phone"
          placeholder="请输入联系方式"
        />
      </el-form-item>
      <el-form-item label="登录账号:">
        <el-input
          clearable
          v-model="form.staffCode"
          :maxlength="11"
          placeholder="请输入登录账号"
        />
      </el-form-item>
      <el-form-item label="登录密码:">
        <el-input
          show-password
          clearable
          v-model="form.password"
          placeholder="请输入登录密码"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { staffAdd, staffUpdate } from '@/api/staffManage/employeeList';
  import {JSEncrypt} from "jsencrypt";
  export default {
    name: "org-user-add",
    props: {
      // 编辑回显的数据
      data: Object,
    },
    data() {
      return {
        // 弹窗是否打开
        visible: false,
        loading:false,
        form:{
          merchantId: this.$store.state.user.info.merchantId,  // 商家id
          username: this.$store.state.user.info.username // 商户名称（id）
        }
      };
    },
    methods: {
      show(){
        console.log("this.data",this.data)
        /*if(this.data){
          this.data.password=this.decrypt(this.data.password)
          console.log("this.decrypt(this.data.password)",this.decrypt(this.data.password))
        }*/
        this.form=this.data? this.data: this.form
        this.updateVisible(true)
      },
      /* 更新visible */
      updateVisible(value) {
        this.visible=value
      },
      decrypt(msg) { // 解密
        // const privateKey = '...' // 私钥
        const decrypt = new JSEncrypt()
        let pk = this.$store.state.signature.pk
        decrypt.setPrivateKey(pk)
        // decrypt.setPrivateKey(privateKey)
        const decryptMsg = decrypt.decrypt(msg)
        console.log("msg",msg,'decryptMsg',decryptMsg)
        return decryptMsg
      },
      save(){
        let reg = /^[\S]{6,18}$/
        let phoneReg = /^1[3,4,5,6,7,8,9][0-9]{9}$/
        let min= /[a-zA-Z0-9]{6,}/
        if(!this.form.staffName){
          this.$message.error('请输入员工姓名')
          return
        }
        if(!this.form.phone){
          this.$message.error('请输入联系方式')
          return
        }
        if(phoneReg.test(this.form.phone)==false){
          this.$message.error('请输入正确的联系方式')
          return
        }
        if(!this.form.staffCode){
          this.$message.error('请输入登录账号')
          return
        }
        // if(min.test(this.form.staffCode)==false){
        if(phoneReg.test(this.form.staffCode)==false){
          this.$message.error('登录账号为手机号，请输入正确的手机号')
          return
        }
        if(!this.form.password){
          this.$message.error('请输入登录密码')
          return
        }
        if(reg.test(this.form.password)==false){
          this.$message.error('密码必须为6-18位非空白字符')
          return
        }
        let apis = this.data ? staffUpdate : staffAdd;
        if(!this.data){
          this.form.status=1
        }
        else{
          delete this.form.status
        }
        let hint = this.data ? '编辑员工成功' : '添加员工成功'
        // 员工端，不需要加密
        /*let encryptor = new JSEncrypt()
        let pk = this.$store.state.signature.pk
        encryptor.setPublicKey(pk)
        // 密码加密
        this.form.password = encryptor.encrypt(this.form.password)*/
        apis(this.form)
          .then((data) => {
            console.log("data--------",data)
            this.$message.success(hint)
            this.search();
            this.updateVisible(false)
          })
          .catch((e) => {
            this.$message.error(e);
          });
      },
      /* 搜索 */
      search() {
        this.$emit('search', this.where);
      },
      /*  重置 */
      reset() {
        this.where = { ...this.defaultWhere };
        this.search();
      }
    }
  }
</script>

<style scoped>

</style>
